package com.lagou.edu.service.impl;

import com.lagou.edu.service.EmailService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Service
public class EmailServiceImpl implements EmailService {

    /**
     * 配置文件中我的163邮箱服务host
     */
    @Value("${mail.smtp.host}")
    private String mailHost;

    @Override
    public boolean sendSimpleMail(String to, String subject, String content) {
        try {
            Properties properties = new Properties();
            //("mail.host","smtp.163.com" );
            properties.put("mail.smtp.host", mailHost);
            //
            properties.put("mail.transport.protocol", "smtp");
            //("mail.smtp.auth", true);
            properties.put("mail.smtp.auth", true);

            //使用java发送邮件5步骤
            //1.创建sesssion
            Session session = Session.getInstance(properties);
            //开启session的调试模式，可以查看当前邮件发送状态
            session.setDebug(true);

            //2.通过session获取Transport对象（发送邮件的核心API）
            Transport transport = session.getTransport();
            //3.通过邮件用户名密码链接，记得将xxx换成自己的邮箱授权码
            transport.connect("29193@163.com", "xxx");
            //transport.connect(mailFrom, passwordMailFrom);
            //4.创建邮件
            Message textMail = createTextMail(session, "ttudedong@163.com", to, subject, content);
            //5.发送电子邮件
            transport.sendMessage(textMail, textMail.getAllRecipients());
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    public MimeMessage createTextMail(Session session, String mailFrom, String mailTo, String mailTitle, String mailContent)
            throws MessagingException {
        //创建邮件对象
        MimeMessage message = new MimeMessage(session);
        //设置发件人 "29193@163.com"
        message.setFrom(new InternetAddress(mailFrom));
        //设置收件人 "811622944@qq.com"
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        //设置邮件标题
        message.setSubject(mailTitle);
        //设置邮件内容
        message.setContent(mailContent, "text/html;charset=UTF-8");
        return message;
    }
}
